package com.hejia.bulbul.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hejia.alauda.entity.SaleActivityDevice;
import com.hejia.alauda.service.SaleActivityService;
import com.hejia.alauda.utils.HttpResponse;

@Controller
@RequestMapping("saleactivitydevice")
public class SaleActivityDeviceController extends BaseController {

	@Resource
	private SaleActivityService saleActivityDeviceService;

	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse add(SaleActivityDevice saleActivityDevice) {
		HttpResponse response = new HttpResponse();
		try {
			saleActivityDevice.setCreateDate(new Date());
			saleActivityDevice.setModifyDate(new Date());

			Map<String, Object> map = new HashMap<String, Object>();
			if (saleActivityDeviceService
					.addSaleActivityDevice(saleActivityDevice)) {
				map.put("id", saleActivityDevice.getId());
				map.put("device", saleActivityDevice.getDevice());
				map.put("url", saleActivityDevice.getUrl());
				map.put("banner", saleActivityDevice.getBanner());
				map.put("displayOnBanner",
						saleActivityDevice.isDisplayOnBanner());
				map.put("displayInCenter",
						saleActivityDevice.isDisplayInCenter());
				map.put("overdueHide", saleActivityDevice.isOverdueHide());

				response.setCode("201");
				response.setStatus("success");
				response.setMessage("添加活动配置成功");
				response.setData(map);
			} else {
				response.setCode("400");
				response.setStatus("error");
				response.setMessage("添加活动配置失败");
				response.setData("");
			}

		} catch (Exception e) {
			response.setCode("400");
			response.setStatus("error");
			response.setMessage("添加活动配置失败");
			response.setData("");
		}

		return response;
	}

	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse delete(@PathVariable long id) {
		HttpResponse response = new HttpResponse();
		try {
			SaleActivityDevice saleActivityDevice = new SaleActivityDevice();
			saleActivityDevice.setId(id);
			if (saleActivityDeviceService
					.deleteSaleActivityDevice(saleActivityDevice)) {
				response.setCode("201");
				response.setStatus("success");
				response.setMessage("删除活动配置成功");
				response.setData(id);
			} else {
				response.setCode("400");
				response.setStatus("error");
				response.setMessage("删除活动配置失败");
				response.setData("");
			}

		} catch (Exception e) {
			response.setCode("400");
			response.setStatus("error");
			response.setMessage("删除活动配置失败");
			response.setData("");
		}

		return response;
	}

	@RequestMapping(value = "showupdate/{id}", method = RequestMethod.GET)
	public String showupdate(@PathVariable long id, ModelMap model) {
		SaleActivityDevice saleActivityDevice = saleActivityDeviceService
				.findSaleActivityDeviceById(id);
		model.put("saleActivityDevice", saleActivityDevice);
		return "/product/saleactivity/saupdate";
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse update(SaleActivityDevice saleActivityDevice,
			@PathVariable long id) {
		HttpResponse response = new HttpResponse();
		try {
			SaleActivityDevice saleActivityDevice2 = saleActivityDeviceService
					.findSaleActivityDeviceById(id);
			saleActivityDevice2.setModifyDate(new Date());
			saleActivityDevice2.setDevice(saleActivityDevice.getDevice());
			saleActivityDevice2.setUrl(saleActivityDevice.getUrl());
			saleActivityDevice2.setBanner(saleActivityDevice.getBanner());
			saleActivityDevice2.setDisplayOnBanner(saleActivityDevice
					.isDisplayOnBanner());
			saleActivityDevice2.setDisplayInCenter(saleActivityDevice
					.isDisplayInCenter());
			saleActivityDevice2.setOverdueHide(saleActivityDevice
					.isOverdueHide());

			Map<String, Object> map = new HashMap<String, Object>();
			if (saleActivityDeviceService
					.modifySaleActivityDevice(saleActivityDevice2)) {
				map.put("id", saleActivityDevice.getId());
				map.put("device", saleActivityDevice.getDevice());
				map.put("url", saleActivityDevice.getUrl());
				map.put("banner", saleActivityDevice.getBanner());
				map.put("displayOnBanner",
						saleActivityDevice.isDisplayOnBanner());
				map.put("displayInCenter",
						saleActivityDevice.isDisplayInCenter());
				map.put("overdueHide", saleActivityDevice.isOverdueHide());

				response.setCode("201");
				response.setStatus("success");
				response.setMessage("修改活动配置成功");
				response.setData(map);
			} else {
				response.setCode("400");
				response.setStatus("error");
				response.setMessage("修改活动配置失败");
				response.setData("");
			}

		} catch (Exception e) {
			response.setCode("400");
			response.setStatus("error");
			response.setMessage("修改活动配置失败");
			response.setData("");
		}

		return response;
	}

}
